package com.huobao.yunying.activitys;

import java.util.Map;

import org.json.JSONException;
import org.simple.eventbus.EventBus;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys_log_reg.Act_Login;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageView;

public class Act_RegYouli extends HuobaoBaseActivity {

	@ViewInject(R.id.btn_1)
	private ImageView btn_lingqu;
	@ViewInject(R.id.webview)
	private WebView webview;
	private boolean has_login, is_receive, is_cart;
	private int quantity, goods_id, spec_id;

	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_regyouli;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		mAbTitleBar.setVisibility(View.GONE);
		webview.setBackgroundColor(Color.parseColor("#64ffffff"));
		getData();
		btn_lingqu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (SessionUtils.checkLogin(context)) {
					if (is_receive) {
						BaseDialog.showAlert("您已经领取过了，请勿重复领取！", context);
					} else {
						if (is_cart) {
							EventBus.getDefault().post("", "ShowGouwuche");
							finish();
						} else {
							receiver();
						}
					}
				} else {
					Intent intent = new Intent(context, Act_Login.class);
					startActivity(intent);
				}
			}
		});
	}

	protected void receiver() {
		Map<String, String> map = HttpUtil.getParams("app", "cart", "act", "add", "id", goods_id + "", "spec_id",
				spec_id + "", "quantity", quantity + "");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				EventBus.getDefault().post("", "ShowGouwuche");
				finish();
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void getData() {
		Map<String, String> map = HttpUtil.getParams("app", "promotion", "act", "reggift");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				try {
					org.json.JSONObject ob = new org.json.JSONObject(result);
					ob = ob.getJSONObject("retval");
					String gift_desc = ob.getString("gift_desc");
					webview.loadDataWithBaseURL(null, gift_desc, "text/html", "utf-8", null);
					has_login = ob.getBoolean("has_login");
					is_receive = ob.getBoolean("is_receive");
					is_cart = ob.getBoolean("is_cart");
					quantity = ob.getInt("quantity");
					goods_id = ob.getInt("goods_id");
					spec_id = ob.getInt("spec_id");
				} catch (JSONException e) {
					LogUtil.e(e.toString());
				}
//				JSONObject jsonObject = JSON.parseObject(result);
//				jsonObject = jsonObject.getJSONObject("retval");
//				String gift_desc = jsonObject.getString("gift_desc");
//				webview.loadDataWithBaseURL(null, gift_desc, "text/html", "utf-8", null);
//				has_login = jsonObject.getBooleanValue("has_login");
//				is_receive = jsonObject.getBooleanValue("is_receive");
//				is_cart = jsonObject.getBooleanValue("is_cart");
//				quantity = jsonObject.getIntValue("quantity");
//				goods_id = jsonObject.getIntValue("goods_id");
//				spec_id = jsonObject.getIntValue("spec_id");
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}
}
